package org.example.system.controller.system;

import lombok.RequiredArgsConstructor;
import org.example.common.core.constants.UserConstants;
import org.example.common.core.domain.R;
import org.example.common.core.utils.page.Pager;
import org.example.common.core.utils.page.SearchParam;
import org.example.common.excel.utils.ExcelUtil;
import org.example.common.log.annotation.OperatorLog;
import org.example.common.log.enums.BusinessType;
import org.example.common.security.annotation.PreAuthorize;
import org.example.system.domain.bo.SysPostBo;
import org.example.system.domain.vo.SysPostVo;
import org.example.system.service.ISysPostService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 岗位信息操作处理
 *
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/post")
public class SysPostController   {

    private final ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize(hasAnyPermi = "system:post:list")
    @PostMapping("/list")
    public Pager<SysPostVo> list(@RequestBody Pager pageQuery) {
        return postService.selectPagePostList(pageQuery);
    }

    /**
     * 导出岗位列表
     */
    @OperatorLog(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasAnyPermi = "system:post:export")
    @PostMapping("/export")
    public void export(@RequestBody Pager pageQuery, HttpServletResponse response) {
        List<SysPostVo> list = postService.selectPostList(pageQuery);
        ExcelUtil.exportExcel(list, "岗位数据", SysPostVo.class, response);
    }

    /**
     * 根据岗位编号获取详细信息
     *
     * @param postId 岗位ID
     */
    @PreAuthorize(hasAnyPermi = "system:post:query")
    @GetMapping(value = "/{postId}")
    public R<SysPostVo> getInfo(@PathVariable Long postId) {
        return R.ok(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize(hasAnyPermi = "system:post:add")
    @OperatorLog(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody SysPostBo post) {
        if (!postService.checkPostNameUnique(post)) {
            return R.fail("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return R.fail("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return R.toAjax(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize(hasAnyPermi = "system:post:edit")
    @OperatorLog(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody SysPostBo post) {
        if (!postService.checkPostNameUnique(post)) {
            return R.fail("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return R.fail("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        } else if (UserConstants.POST_DISABLE.equals(post.getStatus())
            && postService.countUserPostById(post.getPostId()) > 0) {
            return R.fail("该岗位下存在已分配用户，不能禁用!");
        }
        return R.toAjax(postService.updatePost(post));
    }

    /**
     * 删除岗位
     *
     * @param postIds 岗位ID串
     */
    @PreAuthorize(hasAnyPermi = "system:post:remove")
    @OperatorLog(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public R<Void> remove(@PathVariable Long[] postIds) {
        return R.toAjax(postService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public R<List<SysPostVo>> optionselect() {
        Pager<SysPostBo> postBoPager=new Pager<>();
        postBoPager.getSearchParams().put(String.format("%s%s", SearchParam.SEARCH_EQ,"status"),UserConstants.POST_NORMAL);
        List<SysPostVo> posts = postService.selectPostList(postBoPager);
        return R.ok(posts);
    }
}
